/**
 * @description:
 * @Author: wuzhenbin
 * @CreateTime: 2025-02-14
 */
public class leetcode_160 {
    public static void main(String[] args) {

    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lengthA = 0;
        int lengthB = 0;
        ListNode ptrA = headA;
        ListNode ptrB = headB;
        while (ptrA != null) {
            ptrA = ptrA.next;
            lengthA++;
        }
        while (ptrB != null) {
            ptrB = ptrB.next;
            lengthB++;
        }
        ptrA = headA;
        ptrB = headB;
        if(lengthA>lengthB){
            int count = lengthA - lengthB;
            while(count>0){
                ptrA = ptrA.next;
                count--;
            }
        }
        else {
            int count = lengthB - lengthA;
            while(count>0){
                ptrB = ptrB.next;
                count--;
            }
        }
        while(ptrA!=null){
            if(ptrA.equals(ptrB))return ptrA;
            ptrA = ptrA.next;
            ptrB = ptrB.next;
        }
        return null;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
